<template>
  <div style="width: 100%; height: 100%">
    <div :id="className" style="width: 100%; height: 100%" />
  </div>
</template>

<script>
  import * as echarts from 'echarts'

  export default {
    props: {
      // 过滤器
      // 类名
      className: {
        type: String,
        default: 'chart',
      },
      width: {
        type: String,
        default: '100%',
      },
      height: {
        type: String,
        default: '100%',
      },
      barData: {
        // 数据
        type: Array,
        default: Array,
      },
      barcolorData: {
        // 颜色值
        type: Array,
        default: Array,
      },
    },
    data() {
      return {
        chart: null,
      }
    },
    // 监听数据变化
    watch: {
      barData: {
        deep: true,
        handler(val) {
          this.drawPid(val)
        },
      },
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    mounted() {
      this.$nextTick(function () {
        this.drawPid(this.barData)
      })
    },
    methods: {
      drawPid(data) {
        console.log(data)
        var xData = []
        var seData = []
        if (data.length > 0) {
          data.forEach(item => {
            xData.push(item.name)
            seData.push(item.value)
          })
        }
        // 基于准备好的dom，初始化echarts实例
        var option = {
          title: {
            text: '企业名称 600021',
            textStyle: {
              fontSize: 14,
            },
          },
          grid: {
            top: '12%',
            left: '10%',
            right: '4%',
            height: '74%',
          },
          tooltip: {},
          legend: {
            // data: ['销量']
          },
          xAxis: {
            data: xData, // ['衬衫', '羊毛衫', '雪纺衫']
          },
          yAxis: {},
          series: [
            {
              // name: '销量',
              type: 'line',
              data: seData,
            },
          ],
        }
        this.chart = echarts.init(document.getElementById(this.className))
        // 使用刚指定的配置项和数据显示图表。
        this.chart.setOption(option)
      },
    },
  }
</script>
